const $ = layui.jquery;
const form = layui.form;
const layer = layui.layer;
const upload = layui.upload;
const dropdown = layui.dropdown;
const tree = layui.tree;

$(()=>{
    // 数据回显
    displayGoods();
})
//表单回显商品数据
function displayGoods() {
    const url = ctx + "/sys/api/v1/goods/id";
    $.ajax({
        url,
        method: "get",
        data: {
            id
        },
        success(resp) {
            if (resp.success) {
                let goods = resp.data;
                form.val("form", goods);

                //类别赋值
                if(goods.category){
                    $("[name=categoryName]").val(goods.category.name);
                    $("[name=categoryId]").val(goods.category.id);
                }

                //回显商品图片
                if (goods.pic) {
                    $(".pic-upload-preview").css("background-image", "url(" + (ctx + "/assets/" + goods.pic) + ")");
                    sessionStorage.setItem("pic-url", goods.pic);
                }
                //回显商品副图片
                if (goods.pic1) {
                    $(".pic1-upload-preview").css("background-image", "url(" + (ctx + "/assets/" + goods.pic1) + ")");
                    sessionStorage.setItem("pic1-url", goods.pic1);
                }
                //回显细节图片
                if (goods.otherPics.length > 0) {
                    for (let pic of goods.otherPics) {
                        let url = ctx + "/assets/" + pic;
                        let $img = $("<div class='goods-pics'></div>").css("background-image", "url(" + (url) + ")").data("url", pic);
                        $("#pics-upload-preview").append($img);
                    }
                }
                // 回显富文本，只用div append就行
                $("#goods-detail").html(goods.detail);

                // 设置所有输入框和下拉框为不可编辑
                $("#form input").prop("readonly", true); // 设置所有输入框为只读
                $("#form select").prop("disabled", true); // 设置所有下拉框为不可编辑
                $("#form textarea").prop("readonly", true); // 设置所有文本域为只读


            }
        }
    });
}
